﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using YArchitech.Revit;
using YJKArch.Utils;
using HYLightingEquipmentLay.HallwaySmokeLayout.Data;

namespace HYLightingEquipmentLay.CommonUtils
{
	public class RoomUtils
	{
		public static List<Curve> GetRoomOutBoundary(Room room, SpatialElementBoundaryLocation lcType = 0)
		{
			List<Curve> list = new List<Curve>();
			IList<IList<BoundarySegment>> boundarySegments = room.GetBoundarySegments(new SpatialElementBoundaryOptions
			{
				SpatialElementBoundaryLocation = lcType
			});
			List<BoundarySegment> list2 = null;
			if (boundarySegments.Count > 0)
			{
				list2 = boundarySegments.First<IList<BoundarySegment>>().ToList<BoundarySegment>();
			}
			if (list2 != null && list2.Count > 0)
			{
				foreach (BoundarySegment boundarySegment in list2)
				{
					if (boundarySegment != null)
					{
						list.Add(UnifiedModified.GetBoundarySegmentCurve(boundarySegment));
					}
				}
			}
			return list;
		}

		public static List<Room> GetDocumentRooms(Autodesk.Revit.DB.Document doc)
		{
			List<SpatialElement> list = DocumentElementUtils.GetSpecifyElements<SpatialElement>(doc).ToList<SpatialElement>();
			List<Room> list2 = new List<Room>();
			foreach (SpatialElement spatialElement in list)
			{
				if (spatialElement is Room)
				{
					list2.Add(spatialElement as Room);
				}
			}
			return RoomUtils.GetNotZeroRoom(list2);
		}

		public static List<Room> GetDocumentRooms(Autodesk.Revit.DB.Document doc, Level level)
		{
			List<Room> list = new List<Room>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfCategory(BuiltInCategory.OST_Rooms).WhereElementIsNotElementType();
			foreach (Element element in filteredElementCollector)
			{
				Room room = element as Room;
				Level hylevel = room.GetYJKLevel();
				double num = 0.0;
				if (hylevel != null)
				{
					num = hylevel.Elevation;
				}
				if (Math.Abs(level.Elevation - num) < 0.0001)
				{
					list.Add(room);
				}
			}
			return RoomUtils.GetNotZeroRoom(list);
		}

		public static List<Room> GetViewRooms(Autodesk.Revit.DB.Document doc, ElementId viewId)
		{
			return RoomUtils.GetNotZeroRoom(DocumentElementUtils.GetSpecifyElements<Room>(doc, viewId));
		}

		public static List<Room> GetLevelRooms(Autodesk.Revit.DB.Document doc, Level level)
		{
			List<Room> documentRooms = RoomUtils.GetDocumentRooms(doc);
			List<Room> list = new List<Room>();
			foreach (Room room in documentRooms)
			{
				if (room.LevelId.IntegerValue == level.Id.IntegerValue)
				{
					list.Add(room);
				}
			}
			return list;
		}

		public static List<RoomBoundary> GetRoomBoundarys(List<Room> lstRoom)
		{
			List<RoomBoundary> list = new List<RoomBoundary>();
			foreach (Room room in lstRoom)
			{
				RoomBoundary item = new RoomBoundary(RoomUtils.GetRoomOutBoundary(room, 0));
				list.Add(item);
			}
			return list;
		}

		public static List<Room> GetNotZeroRoom(List<Room> lstRoom)
		{
			List<Room> list = new List<Room>();
			foreach (Room room in lstRoom)
			{
				if (!Geometry.Lessthan_Or_Equal(room.Area, 0.0, 1E-05))
				{
					list.Add(room);
				}
			}
			return list;
		}

		public static List<Room> GetNameContainsRooms(List<Room> lstRoom, string strContains)
		{
			List<Room> list = new List<Room>();
			if (string.IsNullOrEmpty(strContains))
			{
				return list;
			}
			foreach (Room room in lstRoom)
			{
				if (room.Name.Contains(strContains))
				{
					list.Add(room);
				}
			}
			return list;
		}
	}
}
